package com.hzdl.controller;

import com.hzdl.bean.Cost;
import com.hzdl.dao.CostDao;
import com.hzdl.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author: bxc
 * @date: 2018/9/7 0007
 * @description:
 */
@Controller
@RequestMapping("/cost")
public class CostController {

    @Autowired
    private CostDao costDao;

    @RequestMapping("/getAllCost")
    public String getAllCost(int aid,Model model){
        List<Cost> costs = costDao.getAllCost(aid);
        model.addAttribute("costs",costs);
        String nowtime = DateUtils.getNowTime();
        model.addAttribute("nowtime",nowtime);
        return "costHistory";
    }

    @RequestMapping("/topay")
    public String toPay(){
        return "pay";
    }

    @RequestMapping(value = "/pay",method = RequestMethod.POST)
    public String pay(HttpServletRequest request,Model model){
        String costid = request.getParameter("hidden");
        int coid = Integer.parseInt(costid);
        String money = request.getParameter("paymoney");
        String paytime = DateUtils.getNowTime();
        Date time = DateUtils.convert(paytime);
        int number=(int)(Math.random()*100000000);
        costDao.updatePay(coid,number,time,money);
        List<Cost> costs2 = costDao.getAllCosts();
        model.addAttribute("costs2",costs2);
        return "payHistory";
    }

    @RequestMapping("/paylist")
    public String payList(Model model){
        List<Cost> costs2 = costDao.getAllCosts();
        model.addAttribute("costs2",costs2);
        return "payHistory";
    }

}
